
package net.atremmedia.gwt.actionjava.examples.bounds.samples.curve;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.display.TextField;
import actionjava.geom.shape.Rectangle2D;

abstract class AbstractBounds extends AJExample
{
	public static final String CURVE_LINE_COLOR = "#FF0000";
	public static final String BOUNDS_LINE_COLOR = "#000000";
	public static final String CP_LINE_COLOR = "#00FF00";

	public static final String P_DOT_COLOR = "#000000";
	public static final String P1_DOT_COLOR = "#FF0000";
	public static final String P2_DOT_COLOR = "#0066FF";
	public static final String CP_DOT_COLOR = "#00FF00";

	private static final double LEGENDA_MARGIN = 10;
	private static final double LEGENDA_Y_OFFSET = 30;
	private static final double LEGENDA_WIDTH = 150;
	private static final double LEGENDA_HEIGHT = 340;
	private static final String LEGENDA_BG_COLOR = "#999999";

	/*
	 * ---------------------------------------------------------------
	 *	Constructor
	 * ---------------------------------------------------------------
	 */

	public AbstractBounds(String name, String description, String exampleSrc, AJExampleOverview base)
	{
		super(name, description, exampleSrc, base);
	}

	/*
	 * ---------------------------------------------------------------
	 *	Public methods
	 * ---------------------------------------------------------------
	 */

	public void drawLegenda()
	{
		Sprite legenda = drawLegendaBG(0, 0, 100, 95);
		double yOffset = drawLegendaLines(15, legenda);
		addChild(legenda);

		legenda = drawLegendaBG(0, yOffset, 100, 130);
		yOffset = drawLegendaDots(15, legenda);
		addChild(legenda);

		Rectangle2D bounds = getBounds();
		if(bounds != null) {
			legenda = drawLegendaBG(0, 245, 100, 130);
			drawLegendaBoundsInfo(String.valueOf(Math.round(bounds.x)), String.valueOf(Math.round(bounds.y)), String.valueOf(Math.round(bounds.width)), String.valueOf(Math.round(bounds.height)), 15, legenda);
			addChild(legenda);
		}
	}

	public Sprite drawLegendaBG(double x, double y, double w, double h)
	{
		Sprite legenda = new Sprite();
		legenda.setX(getWidth() - (w + LEGENDA_MARGIN));
		legenda.setY(y + LEGENDA_MARGIN);

		Graphics legendaGraph = legenda.getGraphics();
		legendaGraph.beginStroke(LEGENDA_BG_COLOR);
		legendaGraph.drawRoundRect(0, 0, w, h, 5);
		legendaGraph.endStroke();

		return legenda;
	}

	public double drawLegendaLines(double yOffset, Sprite legenda)
	{
		Shape legendaLines = new Shape();
		Graphics graph = legendaLines.getGraphics();

		graph.moveTo(LEGENDA_MARGIN, yOffset).setStrokeStyle(3).beginStroke(CURVE_LINE_COLOR).lineTo((LEGENDA_MARGIN + 15), (yOffset + 5)).endStroke();
		drawText((LEGENDA_MARGIN + 25), (yOffset - 3), "Curve", legenda);
		yOffset += LEGENDA_Y_OFFSET;

		graph.moveTo(LEGENDA_MARGIN, yOffset).setStrokeStyle(3).beginStroke(BOUNDS_LINE_COLOR).lineTo((LEGENDA_MARGIN + 15), (yOffset + 5)).endStroke();
		drawText((LEGENDA_MARGIN + 25), (yOffset - 3), "Bounds", legenda);
		yOffset += LEGENDA_Y_OFFSET;

		graph.moveTo(LEGENDA_MARGIN, yOffset).setStrokeStyle(3).beginStroke(CP_LINE_COLOR).lineTo((LEGENDA_MARGIN + 15), (yOffset + 5)).endStroke();
		drawText((LEGENDA_MARGIN + 25), (yOffset - 3), "Control", legenda);
		yOffset += LEGENDA_Y_OFFSET;

		legenda.addChild(legendaLines);

		return yOffset;
	}

	public double drawLegendaDots(double yOffset, Sprite legenda)
	{
		double dotRadius = 6;

		Shape legendaDot = new Shape();
		legendaDot.setX(LEGENDA_MARGIN + 10);
		legendaDot.setY(yOffset + 10);
		Graphics graph = legendaDot.getGraphics();
		graph.moveTo(LEGENDA_MARGIN, yOffset).beginFill(P1_DOT_COLOR).drawCircle(-(dotRadius / 2), -(dotRadius / 2), dotRadius).endFill();
		drawText((LEGENDA_MARGIN + 25), yOffset, "Start", legenda);
		legenda.addChild(legendaDot);
		yOffset += LEGENDA_Y_OFFSET;

		legendaDot = new Shape();
		legendaDot.setX(LEGENDA_MARGIN + 10);
		legendaDot.setY(yOffset + 10);
		graph = legendaDot.getGraphics();
		graph.moveTo(LEGENDA_MARGIN, yOffset).beginFill(P2_DOT_COLOR).drawCircle(-(dotRadius / 2), -(dotRadius / 2), dotRadius).endFill();
		drawText((LEGENDA_MARGIN + 25), yOffset, "End", legenda);
		legenda.addChild(legendaDot);
		yOffset += LEGENDA_Y_OFFSET;

		legendaDot = new Shape();
		legendaDot.setX(LEGENDA_MARGIN + 10);
		legendaDot.setY(yOffset + 10);
		graph = legendaDot.getGraphics();
		graph.moveTo(LEGENDA_MARGIN, yOffset).beginFill(CP_DOT_COLOR).drawCircle(-(dotRadius / 2), -(dotRadius / 2), dotRadius).endFill();
		drawText((LEGENDA_MARGIN + 25), yOffset, "Control", legenda);
		legenda.addChild(legendaDot);
		yOffset += LEGENDA_Y_OFFSET;

		legendaDot = new Shape();
		legendaDot.setX(LEGENDA_MARGIN + 10);
		legendaDot.setY(yOffset + 10);
		graph = legendaDot.getGraphics();
		graph.moveTo(LEGENDA_MARGIN, yOffset).beginFill(P_DOT_COLOR).drawCircle(-(dotRadius / 2), -(dotRadius / 2), dotRadius).endFill();
		drawText((LEGENDA_MARGIN + 25), yOffset, "Point", legenda);
		legenda.addChild(legendaDot);
		yOffset += LEGENDA_Y_OFFSET;

		return yOffset;
	}

	public double drawLegendaBoundsInfo(String xInf, String yInf, String wInf, String hInf, double yOffset, Sprite legenda)
	{
		drawText(LEGENDA_MARGIN, (yOffset - 3), 15, "X", legenda);
		drawText((LEGENDA_MARGIN + 25), yOffset, xInf, legenda);
		yOffset += LEGENDA_Y_OFFSET;

		drawText(LEGENDA_MARGIN, (yOffset - 3), 15, "Y", legenda);
		drawText((LEGENDA_MARGIN + 25), yOffset, yInf, legenda);
		yOffset += LEGENDA_Y_OFFSET;

		drawText(LEGENDA_MARGIN, (yOffset - 3), 15, "W", legenda);
		drawText((LEGENDA_MARGIN + 25), yOffset, wInf, legenda);
		yOffset += LEGENDA_Y_OFFSET;

		drawText(LEGENDA_MARGIN, (yOffset - 3), 15, "H", legenda);
		drawText((LEGENDA_MARGIN + 25), yOffset, hInf, legenda);
		yOffset += LEGENDA_Y_OFFSET;

		return yOffset;
	}

	public void drawText(double x, double y, String text, Sprite legenda)
	{
		TextField textF = new TextField(text);
		textF.setX(x);
		textF.setY(y);
		textF.setSize(12);

		legenda.addChild(textF);
	}

	public void drawText(double x, double y, int fontSize, String text, Sprite legenda)
	{
		TextField textF = new TextField(text);
		textF.setX(x);
		textF.setY(y);
		textF.setSize(fontSize);

		legenda.addChild(textF);
	}

	/*
	 * ---------------------------------------------------------------
	 *	to be overriden methods
	 * ---------------------------------------------------------------
	 */

	public abstract void setBounds();

	public abstract Rectangle2D getBounds();
}
